import { BuyerConfirmAction } from '../actions'
import { BuyerConfirm } from '../Types'
import { 
  GetBuyerConfirmList_PENDING, 
  GetBuyerConfirmList_FULFILLED, 
  GetBuyerConfirmList_PAGE,
  GetSupplierList_PENDING, 
  GetSupplierList_FULFILLED, 
  GetSupplierList_PAGE } from '../Constants'
// 初始化
const initState= {
  loading: false,
  list: [],
  type: 1,
  data: {
    list: [],
    pagination: {}
  }
}
export function BuyerConfirmReducer(state: BuyerConfirm = initState, action: BuyerConfirmAction): BuyerConfirm {
  switch (action.type) {
    case GetBuyerConfirmList_PENDING:
      return { ...state, loading: true }
    case GetBuyerConfirmList_PAGE:
      return { ...state, type: action.payload }
    case GetBuyerConfirmList_FULFILLED:
      let list = state.list
      if (state.type == 1) list = action.payload.list
      else list = [
        ...state.list||[],
        ...action.payload.list
      ]
      return { 
        ...state, 
        loading: false, 
        list
      }
    case GetSupplierList_PENDING:
      return { ...state, loading: true }
    case GetSupplierList_FULFILLED:
      return { 
        ...state, 
        loading: false,
        data: { 
          list: action.payload.list, 
          pagination: { 
            ...state.data.pagination,
            total: action.payload.recordsTotal 
          } 
        } 
      }
    case GetSupplierList_PAGE: 
      return {
        ...state,
        data: { 
          pagination:{
            ...state.data.pagination,
            current: action.payload.current || 1,
            pageSize: action.payload.pageSize || 10
          }
        } 
      }
  }
  return state
}